﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ClassLibrary.Data;
using System.Data;
using ClassLibrary.Entity;

namespace ClassLibrary.DAO
{
    public class TblModelDAO
    {
        private DBService _db = null;
        public TblModelDAO(string ConnectionString)
        {
            _db = new DBService(ConnectionString);
        }
        public DataTable Model_InUp(TblModel model, int mode)
        {
            DataTable flag = null;
            try
            {
                flag = _db.ExeSP_DataTable("TblPhoneBrandModel_InUp",
                    new string[] {"@IDModel","@Model","@BrandID","@Mode"},
                    new object[] {model.IDModel, model.Model, model.BrandID, mode}, CommandType.StoredProcedure);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return flag;
        }
        public bool check_model(string Model)
        {
            bool flag = false;
            try
            {
                DataTable dt = _db.ExeSP_DataTable("select * from TblModel where Model = @Model",
                   new string[] { "@Model" },
                   new object[] { Model },
                   CommandType.Text);
                if (dt.Rows.Count > 0)
                {
                    flag = true;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return flag;
        }
        public DataTable GetModelById(int id)
        {
            DataTable dt = null;
            try
            {
                dt = _db.ExeSP_DataTable("select IDModel, Model, IDBrand, BrandName from TblModel a , TblBrandPhone b where  a.BrandID = b.IDBrand and IDModel = @IDModel",
                    new string[] { "@IDModel" },
                    new object[] { id }, CommandType.Text);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return dt;

        }

        public DataTable GetModelByIdBrand(int id)
        {
            DataTable dt = null;
            try
            {
                dt = _db.ExeSP_DataTable("select * from TblModel where BrandID = @BrandID order by Model ASC",
                    new string[] { "@BrandID" },
                    new object[] { id }, CommandType.Text);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return dt;

        }

        public DataTable GetModel()
        {
            DataTable dt = null;
            try
            {
                dt = _db.ExeSP_DataTable("select IDModel, Model, BrandID, BrandName from TblModel a, TblBrandPhone b where a.BrandID = b.IDBrand order by IDModel ASC", false, CommandType.Text);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return dt;

        }

        public DataTable Delete(int IDModel)
        {
            DataTable dt = null;
            try
            {
                dt = _db.ExeSP_DataTable("delete TblModel where IDModel = @IDModel ",
                    new string[] { "@IDModel" },
                    new object[] { IDModel }, CommandType.Text);

            }
            catch (Exception ex)
            {
                throw ex;
            }
            return dt;
        }
    }
}
